package cn.edu.bzu.ie.sf.mealsmisweb.mapper;

import cn.edu.bzu.ie.sf.mealsmisweb.entity.Commition;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.mapper
 * @Description : 经委会数据操作对象
 * @Create on : 2021/8/15 17:20
 * @author: Feng Song 宋锋
 **/
@Repository
public interface CommitionMapper extends Mapper<Commition> {
    /**
     * 获取某个部门下某个周的所有经委会信息，用于大队用户的管理
     * @param pid 父部门id
     * @param year 年份
     * @param week 所在周数
     * @return List<Commition> 集合实例
     */
    @Select("select a.*,c.uname from commition a " +
            " INNER JOIN department b on a.depid=b.id " +
            " inner join userinfo c on a.uid=c.id " +
            " where b.pid=#{pid} and a.year=#{year} and a.`week`=#{week}")
    List<Commition> getCommitionListByPidAndWeek(int pid, String year, int week);

    /**
     * 获取某个部门的所有的周食谱的数量
     * @param depid 部门的id
     * @return int 数量
     */
    @Select("select count(1) as sl from commition where depid=#{depid}")
    long getCountByDepid(int depid);

    /**
     * 获取某个部门下的所有的
     * @param depid 部门id
     * @param firstNum 当前的页码的第1记录
     * @param pageSize 每个页面的计算条数
     * @return List<Commition> 集合实例
     */
    @Select("select a.*, b.uname from commition a inner join userinfo b on a.uid=b.id where a.depid=#{depid} order by a.id desc limit #{firstNum}, #{pageSize}")
    List<Commition> getCommitionPageListByDepid(int depid, int firstNum, int pageSize);

    /**
     * 获取载个部门某个周的经委会信息，用于中队用户的上传与维护
     * 也可以用于支队与大队用户的查询
     * @param depid 部门id
     * @param year 所在年份
     * @param week 周数
     * @return Commition 实例
     */
    @Select("select a.*,b.uname from commition a inner join userinfo b on a.uid=b.id where a.depid=#{depid} and a.year=#{year} and a.`week`=#{week}")
    Commition getCommitionByDepidAndWeek(int depid, String year, int week);

    /**
     * 添加经委会信息
     * @param item 要添加的周食谱信息，无id的值
     * @return int 影响的行数
     */
    @Insert("insert into commition(uid, depid, pic, year, `week`, weekstr, create_dt) values(#{uid}, #{depid}, #{pic}, " +
            " #{year}, #{week}, #{weekstr},  #{create_dt})")
    @Options(useGeneratedKeys=true, keyProperty="id")
    int addCommition(Commition item);

    /**
     * 修改经委会信息
     * @param item 要修改的经委会信息
     * @return int 影响的行数
     */
    @Update("update Commition set uid=#{uid}, pic=#{pic}, create_dt=#{create_dt} where id=#{id}")
    int editCommition(Commition item);

    /**
     * 删除经委会信息
     * @param id 要删除对象的id
     * @return int 影响的行数
     */
    @Delete("delete from commition where id=#{id}")
    int delCommition(int id);
}
